$(function () {
    $("#top-html").load("topfile.html", function () {
        $("#nav-info-1").text("产品中心");
        $("#nav-info-2").text("PRODUCT");
        $("#location-2").text("产品中心");
    });
    $("#foot-html").load("footfile.html");
    myajax("/getproductcategorysurvival.json", "get", {}, function (result) {
        if (result.status == 200) {
            let pcate_html = '';
            for (let pcate of result.data) {
                pcate_html += '<div pcid="' + pcate.id + '" onclick="pcateClick(this)">' + pcate.name + '</div>';
            }
            $(".product-content-nav-").html(pcate_html);


            $(".product-content-nav->div").hover(function () {
                checkflag = ($(this).css("background-color") == "rgb(0, 179, 204)");
                $(this).css("background-color", "#00b3cc");

            }, function () {
                if (!checkflag)
                    $(this).css("background-color", "#07192f");
            });

            $(".product-content-nav->div:first-child").trigger("click");
        }
    });
});
var checkflag;
var pagebox;

function pcateClick(obj) {
    checkflag=true;
    $(".product-content-nav->div").css("background-color", '#07192f');
    $(obj).css("background-color", '#00b3cc');
    var cate_id = $(obj).attr("pcid");

    if (pagebox != null) {
        pagebox.destroy();
    }

    $("#product-data").html('<div class="product-content-data"></div><div id="product-page" class="page"></div>');

    $(".product-content-data").empty();

    myajax("/getproductcountbycateid.json", "get", {"cate_id": cate_id}, function (result) {
        if (result.status == 200) {
            pagebox = new CustomPagination('#product-page', {
                total: result.data,//总页数
                changePage: function (pageNum) {//切换页码成功回调
                    myajax("/getproductbycateid.json", "get", {"cate_id": cate_id, "pagenum": pageNum}, function (res) {
                        if (res.status == 200) {
                            let product_html = '';
                            for (let product of res.data) {
                                product_html += '<div>' +
                                    '                <div pid="' + product.id + '" onclick="show_product_info(this)">' +
                                    '                   <img  src="' + imgs_path + product.img + '" height="200px" width="260px"/>' +
                                    '                   <span >' + product.name + '</span>' +
                                    '                </div>' +
                                    '                <div class="product-content-data-text">' + product.describe + '</div>' +
                                    '            </div>'
                            }
                            $(".product-content-data").html(product_html);
                        }
                    });
                }
            });
        }
    });
}

function show_product_info(obj) {
    $("#product-data").load("productinfo.html", function () {
        myajax("/getproductallinfo.json", "get", {"id": $(obj).attr("pid")}, function (result) {
            if (result.status == 200) {
                let product = result.data.product;
                let productinfo = result.data.productInfo;
                $("#img").attr("src", imgs_path + product.img);
                $("#name").html(product.name);
                $("#describe").html(product.describe);

                $("#listprice").html(productinfo.listprice / 100);
                $("#price").html(productinfo.price / 100);
                $("#description").html(productinfo.description);
                $("#sendinfo").html(productinfo.sendinfo);
                $("#cqcinfo").html(productinfo.cqcinfo);
                $("#packageinfo").html(productinfo.packageinfo);

                $("#payment").html(getpaymentinfo(productinfo.payment));
            }
        });
    });
}

function getpaymentinfo(i) {
    switch (i) {
        case 1:
            return "协商决定";
        case 2:
            return "货到付款";
        case 3:
            return "银行转账";
        case 4:
            return "移动支付";
    }
}